package com.easyCms.base.busi;

import com.easyCms.base.common.CodeConst;
import com.easyCms.base.common.RespCode;
import com.easyCms.base.entity.*;
import com.easyCms.base.service.AppApplyListSV;
import com.easyCms.base.service.AppIdentitySV;
import com.easyCms.base.service.CrmUserAuthSV;
import com.easyCms.base.service.CrmUserSV;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class UserAuthBusi {
    private static Logger logger = LoggerFactory.getLogger(UserAuthBusi.class);
    @Autowired
    private AppApplyListSV appApplyListSV;
    @Autowired
    private RegisterUserBusi registerUserBusi;
    @Autowired
    private AppIdentitySV appIdentitySV;
    @Autowired
    private CrmUserSV crmUserSV;
    @Autowired
    private CrmUserAuthSV crmUserAuthSV;

    public List<AppApplyList> getUserApplyList(int pageNumber, int pageSize) {
        return appApplyListSV.getUserApplyList(pageNumber, pageSize);
    }

    public ResultBean editUserApply(int operatorType, Long applyId, String comment, ResultBean result) {
        try {
            AppApplyList applyList = appApplyListSV.findValidById(applyId);
            if (applyList == null) {
                result.setCode(ResultBean.FAIL);
                return result;
            }
            Date now = new Date();
            if (operatorType == 1) {// 同意
                // 1.保存AppIdentity帐号
                AppIdentity appIdentity = new AppIdentity();
                appIdentity.setValidDate(now);
                appIdentity.setLoginName(applyList.getPhoneNumber());
                appIdentity.setPassword(applyList.getPassword());
                appIdentity.setState(CodeConst.State.STATE_VALID);
                appIdentity.setCreateTime(now);
                appIdentity.setHamiCode(registerUserBusi.getIdentityCode(now));
                registerUserBusi.entryptPassword(appIdentity);
                AppIdentity identity = appIdentitySV.save(appIdentity);

                // 4.创建用户
                CrmUser crmUser = new CrmUser();
                crmUser.setHamiCode(appIdentity.getHamiCode());
                crmUser.setUserName(applyList.getUserName());
                crmUser.setPhoneNumber(applyList.getPhoneNumber());
                crmUser.setNick("哆来A梦");// 默认昵称
                crmUser.setHeadImg(
                        "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3965109217,1887678806&fm=27&gp=0.jpg");// 默认头像
                crmUser.setState(CodeConst.State.STATE_VALID);
                crmUserSV.save(crmUser);
                applyList.setApplyStatus(RespCode.Apply_List_State.APPLY_TYPE);
                applyList.setHandleTime(now);
                appApplyListSV.save(applyList);

                CrmUserAuth crmUserAuth = new CrmUserAuth();
                crmUserAuth.setHamiCode(appIdentity.getHamiCode());
                crmUserAuth.setAuthType(CodeConst.USER_AUTH.TEACHER);
                crmUserAuth.setState(CodeConst.State.STATE_VALID);
                crmUserAuthSV.save(crmUserAuth);
                result.setCode(ResultBean.SUCCESS);
                return result;
            } else if (operatorType == 2) {// 拒绝
                applyList.setApplyStatus(RespCode.Apply_List_State.APPLY_FAIL_TYPE);
                applyList.setHandleTime(now);
                applyList.setComment(comment);
                appApplyListSV.save(applyList);
                result.setCode(ResultBean.SUCCESS);
                return result;
            } else {// 删除
                applyList.setState(CodeConst.State.STATE_INVALID);
                appApplyListSV.save(applyList);
                result.setCode(ResultBean.SUCCESS);
                return result;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;

    }

}
